package com.minshenglife.zanzan.service.impl;


import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.AccessTokenDao;
import com.minshenglife.zanzan.entity.AccessToken;
import com.minshenglife.zanzan.entity.EMOUserGroup;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.service.AccessTokenService;


/**
 * 用户信息service
 * @author huodd
 *
 */
@Component
@Transactional
public class AccessTokenServiceImpl implements AccessTokenService{

	@Resource
	AccessTokenDao accessTokenDao;
	
	@Override
	public void insertAccessToken(User user, String accessToken) {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, 30); //30分钟
		
		AccessToken act=new AccessToken();
		act.setAccessToken(accessToken);
		//根据tokenId 获取、AccessToken对象
		act=this.getAccessTokenByToken(act);
		if(act==null){
			AccessToken token=new AccessToken();
			token.setAccessToken(accessToken);
			token.setEmpId(user.getEmpId());
			token.setOpenId(user.getWXCode());
			token.setCreate_time(date);          //创建时间
			token.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.add(token);
		}else{
			act.setCreate_time(date);          //创建时间
			act.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.update(act);
		}
		
	}
	
	@Override
	public void insertAccessTokenOfNotUser(String openId, String accessToken) {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, 30); //30分钟
		
		AccessToken act=new AccessToken();
		act.setAccessToken(accessToken);
		//根据tokenId 获取、AccessToken对象
		act=this.getAccessTokenByToken(act);
		if(act==null){
			AccessToken token=new AccessToken();
			token.setAccessToken(accessToken);
//			token.setEmpId();
			token.setOpenId(openId);
			token.setCreate_time(date);          //创建时间
			token.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.add(token);
		}else{
			act.setCreate_time(date);          //创建时间
			act.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.update(act);
		}
	}

	@Override
	public AccessToken getAccessTokenByToken(AccessToken accessToken) {
		List<AccessToken> list=accessTokenDao.findByExample(accessToken);
		
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public void deletetAccessTokenByToken(AccessToken accessToken) {
		accessTokenDao.deleteByExample(accessToken);
	}

	@Override
	public void updateAccessTokenByToken(AccessToken accessToken) {
		accessTokenDao.update(accessToken);
		
	}

	@Override
	public void insertWXAccessToken(String flag, String access_token) {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND, 7000); //7000秒
		
		AccessToken act=null;
		//根据tokenId 获取、AccessToken对象
		act=this.getWXAccessToken(flag);
		if(act==null){
			AccessToken token=new AccessToken();
			token.setAccessToken(access_token);
			token.setOpenId(flag);
			token.setCreate_time(date);          //创建时间
			token.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.add(token);
		}else{
			act.setAccessToken(access_token);
			act.setCreate_time(date);          //创建时间
			act.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.update(act);
		}
	}

	@Override
	public AccessToken getWXAccessToken(String flag) {
		AccessToken act=new AccessToken();
		act.setOpenId(flag);
		List<AccessToken> list=accessTokenDao.findByExample(act);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public void insertAccessToken(EMOUserGroup usergroup, String accessToken) {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, 30); //30分钟
		
		AccessToken act=new AccessToken();
		act.setAccessToken(accessToken);
		//根据tokenId 获取、AccessToken对象
		act=this.getAccessTokenByToken(act);
		if(act==null){
			AccessToken token=new AccessToken();
			token.setAccessToken(accessToken);
			token.setEmpId(usergroup.getEmpId());
			token.setOpenId(usergroup.getOpenid());
			token.setCreate_time(date);          //创建时间
			token.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.add(token);
		}else{
			act.setCreate_time(date);          //创建时间
			act.setEnd_time(calendar.getTime());//结束时间
			accessTokenDao.update(act);
		}
	}

	
	

}
